package cn.jcet.mapreduce.index;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * @author 鲜磊
 * @Date 2020/12/30 23:57
 */
public class IndexOneMapper extends Mapper<LongWritable,Text,Text,IntWritable> {

    String name;
    @Override
    protected void setup(Context context) throws IOException, InterruptedException {
        // 获取文件名称
        FileSplit inputSplit = (FileSplit) context.getInputSplit();

        name = inputSplit.getPath().getName();
    }

    private Text k = new Text();
    private IntWritable v = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 获取行
        String line = value.toString();
        // 空格切割
        String[] worlds = line.split(" ");
        // 切割出的单词加文件名称
        for (String world : worlds) {
            k.set(world + "---" + name);
            // 写出
            context.write(k,v);
        }
    }
}
